package cn.amebius.simpledocker.util;


import cn.amebius.simpledocker.enums.ErrorCode;
import lombok.Data;

/**
 * 统一返回类
 * @author 阿伟
 * @date 2023/7/27 19:47
 */
@Data
public class Response<T> {

    private String msg;

    private T data;

    /**
     *   错误代码
     *   @apiNote 代码400表示登录异常，会被剔除出系统
     */
    private int code;


    public Response<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public Response<T> setErrorCode(ErrorCode errorCode){
        this.code = errorCode.getCode();
        return this;
    }

    /**
     * 失败的数据
     * @param data 数据
     * @param <T>  泛型
     * @return 失败的包裹数据
     */
    public static <T> Response<T> fail(String msg) {
        final Response<T> tResponseModel = new Response<>();
        tResponseModel.setMsg(msg);
        tResponseModel.setCode(1);
        return tResponseModel;
    }

    /**
     * 成功
     * @param <T> 泛型对象
     * @return 包裹后的数据
     */
    public static <T> Response<T> ok(T t) {
        final Response<T> model = new Response<>();
        model.setData(t);
        model.setCode(0);
        return model;
    }
}
